package business;

import android.content.Context;
import android.content.Intent;

import com.loopj.android.http.RequestParams;

import org.json.JSONException;
import org.json.JSONObject;

import common.AppContext;
import common.Constants;
import shuntianfu.com.shuntianfucompressor.LoginActivity;
import shuntianfu.com.shuntianfucompressor.R;
import utils.CheckNetUtil;
import utils.DataHandler;
import utils.HttpRequestUtil;
import utils.HttpServiceHandler;
import utils.Log;
import utils.SharedPreferencesUtils;

/**
 * Created by xinqi on 2016/3/30
 */
public class LoginManager {
    private final String TAG = "LoginManager";

    private LoginManager() {
    }

    static class LoginManagerHolder {
        static LoginManager loginManager = new LoginManager();
    }

    public static LoginManager getInstance() {
        return LoginManagerHolder.loginManager;
    }

    public void login(final Context context, String name, String pwd, final DataHandler dataHandler) {
        if (!CheckNetUtil.checkNetWork(context)) {
            if (dataHandler != null) {
                dataHandler.onData(Constants.Error.NETWORK_IS_UNREACHABLE, context.getString(R.string.error_net_unconnect), null);
            }
            return;
        }
        // TODO: 2016/3/30
        final HttpServiceHandler httpServiceHandler = new HttpServiceHandler(dataHandler) {
            @Override
            public void onResponse(int code, String reason, Object msgData) {
                if (code == Constants.SERVER_SUCCESS) {
                    try {
                        if (null != dataHandler) {
                            Log.d(TAG, "code=" + code + "reason=" + reason);
                            dataHandler.onData(code, reason, msgData);
                            if (msgData != null) {
                                Log.d(TAG, "msgData=" + msgData);
                            }
                        }

                    } catch (Exception e) {
                        Log.e(e.toString(), e.toString());
                    }
                } else {
                    if (null != dataHandler) {
                        dataHandler.onData(code, reason, null);
                    }
                }
            }

            @Override
            public void onError(int code, String reason, Throwable e) {
                super.onError(code, reason, e);
                Log.e(TAG, "onError code=" + String.valueOf(code) + ",reason=" + reason);
                if (null != getDataHandler()) {
                    getDataHandler().onData(code, reason, null);
                }
            }
        };

        RequestParams requestParams = new RequestParams();
        requestParams.put("userName", name);
        requestParams.put("password", pwd);

        HttpRequestUtil.sendFormRequestAsync(Constants.SERVER_HOST + Constants.USER_LOGIN, requestParams, context, httpServiceHandler);

//        String url = Constants.SERVER_HOST + Constants.USER_LOGIN
//                + "?userName=" + name + "&password=" + pwd;
//
//        Log.e(TAG,url);
//
//        HttpRequestUtil.sendSimpleRequestAsyncGet(url, context, dataHandler);

    }


    public void userAdd(final Context context, String name, String pwd, int role, final DataHandler dataHandler) {
        if (!CheckNetUtil.checkNetWork(context)) {
            if (dataHandler != null) {
                dataHandler.onData(Constants.Error.NETWORK_IS_UNREACHABLE, context.getString(R.string.error_net_unconnect), null);
            }
            return;
        }
        String token = SharedPreferencesUtils.getString(context, Constants.USER_TOKEN, "");
        android.util.Log.i(TAG, "token==" + token);
        String url = Constants.SERVER_HOST + Constants.USER_ADD
                + "?userName=" + name + "&password=" + pwd + "&role=" + role + "&token=" + token;
        HttpRequestUtil.sendSimpleRequestAsyncGet(url, context, dataHandler);

    }

    /**
     * 用户退出
     *
     * @param context
     * @param dataHandler
     */
    public void logOut(final Context context, final DataHandler dataHandler) {
        if (!CheckNetUtil.checkNetWork(context)) {
            if (dataHandler != null) {
                dataHandler.onData(Constants.Error.NETWORK_IS_UNREACHABLE, context.getString(R.string.error_net_unconnect), null);
            }
            return;
        }

        String url = Constants.SERVER_HOST + Constants.USER_LOGOUT;
        HttpRequestUtil.sendSimpleRequestAsyncGet(url, context, dataHandler);

    }

    /**
     * xiugai mima
     *
     * @param context
     * @param dataHandler
     */
    public void modifyPassword(final Context context, String id, String userName, String pwd, String newPwd, final DataHandler dataHandler) {
        if (!CheckNetUtil.checkNetWork(context)) {
            if (dataHandler != null) {
                dataHandler.onData(Constants.Error.NETWORK_IS_UNREACHABLE, context.getString(R.string.error_net_unconnect), null);
            }
            return;
        }
        // TODO: 2016/3/30
        final HttpServiceHandler httpServiceHandler = new HttpServiceHandler(dataHandler) {
            @Override
            public void onResponse(int code, String reason, Object msgData) {
                if (code == Constants.SERVER_SUCCESS) {
                    try {
                        if (null != dataHandler) {
                            Log.d(TAG, "code=" + code + "reason=" + reason);
                            dataHandler.onData(code, reason, msgData);
                            if (msgData != null) {
                                Log.d(TAG, "msgData=" + msgData);
                            }
                        }

                    } catch (Exception e) {
                        Log.e(e.toString(), e.toString());
                    }
                } else {
                    if (null != dataHandler) {
                        dataHandler.onData(code, reason, null);
                    }
                }
            }

            @Override
            public void onError(int code, String reason, Throwable e) {
                super.onError(code, reason, e);
                Log.e(TAG, "onError code=" + String.valueOf(code) + ",reason=" + reason);
                if (null != getDataHandler()) {
                    getDataHandler().onData(code, reason, null);
                }
            }
        };
        String url = Constants.SERVER_HOST + Constants.MODIFY_PASSWORD;

        JSONObject jsonObject = new JSONObject();

//        RequestParams requestParams = new RequestParams();
//        requestParams.put("id", id);
//        requestParams.put("userName", userName);
//        requestParams.put("password", pwd);
//        requestParams.put("newPassword", newPwd);
//
//        HttpRequestUtil.sendFormRequestAsync(url, requestParams, context, httpServiceHandler);

        try {
            jsonObject.put("id", id);
            jsonObject.put("userName", userName);
            jsonObject.put("password", pwd);
            jsonObject.put("newPassword", newPwd);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        Log.e(TAG,"modifyPassword url="+url);

        HttpRequestUtil.sendSimpleRequestAsync(url, jsonObject,context, dataHandler);

    }

    public void clearLoginInfo() {
        AppContext context = AppContext.getInstance();
        SharedPreferencesUtils.saveString(context, Constants.USER_PASSWORD, "");
        SharedPreferencesUtils.saveString(context, Constants.USER_TOKEN, "");

        AppContext.getInstance().setUser(null);

        Intent intent = new Intent(context, LoginActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK|Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(intent);
    }


}
